package com.zeropol2.anyquestion.app.activity;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager.OnPageChangeListener;

import com.zeropol2.anyquestion.R;
import com.zeropol2.anyquestion.app.fragment.MyFragment;
import com.zeropol2.anyquestion.app.fragment.ViewFragment;
import com.zeropol2.anyquestion.app.fragment.WriteFragment;
import com.zeropol2.anyquestion.component.ui.list.ViewPagerLayout;
import com.zeropol2.anyquestion.component.ui.list.ImageTextButtonTab;
import com.zeropol2.anyquestion.component.ui.widget.GlobalNavigationBar;
import com.zeropol2.anyquestion.component.ui.widget.ImageTextButton;

/**
 * @author zeropol2
 * 메인화면 액티비티
 */
/**
 * @author zeropol2
 *
 */
public class MainActivity extends FragmentActivity {

	private GlobalNavigationBar mGnb;
	private ImageTextButtonTab mMainTab;
	private ViewPagerLayout mMainViewPager;
	private ImageTextButtonTab.ItemSelectedListener mItemSelectedListener =
		new ImageTextButtonTab.ItemSelectedListener() {
		@Override
		public void onItemSelected(int position) {
			mGnb.setText(mMainTab.getTabItem(position).getText());
			mMainViewPager.setCurrentItem(position);
			mMainTab.setTabLocation(position);
		}
	};;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mGnb = (GlobalNavigationBar) findViewById(R.id.main_gnb);
		mMainTab = (ImageTextButtonTab) findViewById(R.id.main_main_tab);
		mMainViewPager = (ViewPagerLayout) findViewById(R.id.main_view_pager);
		
		//초기화
		initMainTab();
		initMainViewPager();
	}

	
	/**
	 * 탭 초기화
	 */
	private void initMainTab() {
		List<ImageTextButton> list = new ArrayList<ImageTextButton>();
		
		ImageTextButton tabItem;
		
		//첫번째 탭(조회)
		tabItem = new ImageTextButton(this);
		tabItem.setImage(getResources().getDrawable(R.drawable.icon_view_selector));
		tabItem.setText(getResources().getString(R.string.view));
		list.add(tabItem);

		//두번째 탭(글쓰기)
		tabItem = new ImageTextButton(this);
		tabItem.setImage(getResources().getDrawable(R.drawable.icon_write_selector));
		tabItem.setText(getResources().getString(R.string.write));
		list.add(tabItem);
		
		//세번째 탭(MY)
		tabItem = new ImageTextButton(this);
		tabItem.setImage(getResources().getDrawable(R.drawable.icon_my_selector));
		tabItem.setText(getResources().getString(R.string.my));
		list.add(tabItem);
		
		mMainTab.setTabItem(list);
		mMainTab.setItemSelectedListener(mItemSelectedListener);
		mItemSelectedListener.onItemSelected(0);
		mMainTab.setTabLocation(0);

	}
	
	
	/**
	 *뷰페이저 초기화 
	 */
	private void initMainViewPager() {
		List<Fragment> fragmentList;
		fragmentList = new ArrayList<Fragment> ();
		fragmentList.add(new ViewFragment());
		fragmentList.add(new WriteFragment());
		fragmentList.add(new MyFragment());
		mMainViewPager.setLayoutItem(fragmentList);
		
		mMainViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override 
			public void onPageSelected(int position) {
				mItemSelectedListener.onItemSelected(position);
			}
			@Override 
			public void onPageScrolled(int position, float positionOffest, int positionOffsetPixels) {}
			@Override 
			public void onPageScrollStateChanged(int state) {}
		});
		
	}
	
	

}
